//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.print.options;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import org.jpgolay.me.preferences.PreferencesCRUD;
import org.jpgolay.me.util.Parameter;

// Referenced classes of package org.jpgolay.me.print.options:
//            PrintOptionsGUI, PrintPiecesPanel
public class PrintOptionsController extends PrintOptionsGUI {

    public PrintOptionsController(Parameter param) {
        write = false;
        this.param = param;
        initLaw();
        initCustomerQuote();
        initCustomerOrder();
        initCustomerDelivery();
        initCustomerBill();
        initCustomerAsset();
        initSupplierOrder();
        initSupplierBill();
        initSupplierAsset();
        initListener();
    }

    private void initLaw() {
        if (param.getAccessLaw().length() >= 5 && param.getAccessLaw().charAt(5) == '1') {
            write = true;
        }
    }

    private void initCustomerQuote() {
        getCustomerQuotePrintPieces().getList().setListData(list);
        getCustomerQuotePrintPieces().setLaw(param.getPrintCustomerQuoteLaw());
    }

    private void initCustomerOrder() {
        getCustomerOrderPrintPieces().getList().setListData(list);
        getCustomerOrderPrintPieces().setLaw(param.getPrintCustomerOrderLaw());
    }

    private void initCustomerDelivery() {
        getCustomerDeliveryPrintPieces().getList().setListData(list);
        getCustomerDeliveryPrintPieces().setLaw(param.getPrintCustomerDeliveryLaw());
    }

    private void initCustomerBill() {
        getCustomerBillPrintPieces().getList().setListData(list);
        getCustomerBillPrintPieces().setLaw(param.getPrintCustomerBillLaw());
    }

    private void initCustomerAsset() {
        getCustomerAssetPrintPieces().getList().setListData(list);
        getCustomerAssetPrintPieces().setLaw(param.getPrintCustomerAssetLaw());
    }

    private void initSupplierOrder() {
        getSupplierOrderPrintPieces().getList().setListData(list);
        getSupplierOrderPrintPieces().setLaw(param.getPrintSupplierOrderLaw());
    }

    private void initSupplierBill() {
        getSupplierBillPrintPieces().getList().setListData(list);
        getSupplierBillPrintPieces().setLaw(param.getPrintSupplierBillLaw());
    }

    private void initSupplierAsset() {
        getSupplierAssetPrintPieces().getList().setListData(list);
        getSupplierAssetPrintPieces().setLaw(param.getPrintSupplierAssetLaw());
    }

    private void initListener() {
        getSaveB().setEnabled(write);
        getSaveB().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionSave();
            }
        });
    }

    private void actionSave() {
        PreferencesCRUD crud = new PreferencesCRUD();
        crud.setPrintCustomerAssetLaw(getCustomerAssetPrintPieces().getLaw());
        crud.setPrintCustomerBillLaw(getCustomerBillPrintPieces().getLaw());
        crud.setPrintCustomerDeliveryLaw(getCustomerDeliveryPrintPieces().getLaw());
        crud.setPrintCustomerOrderLaw(getCustomerOrderPrintPieces().getLaw());
        crud.setPrintCustomerQuoteLaw(getCustomerQuotePrintPieces().getLaw());
        crud.setPrintSupplierAssetLaw(getSupplierAssetPrintPieces().getLaw());
        crud.setPrintSupplierBillLaw(getSupplierBillPrintPieces().getLaw());
        crud.setPrintSupplierOrderLaw(getSupplierOrderPrintPieces().getLaw());
        crud.update();
        param.setPrintCustomerAssetLaw(crud.getPrintCustomerAssetLaw());
        param.setPrintCustomerBillLaw(crud.getPrintCustomerBillLaw());
        param.setPrintCustomerDeliveryLaw(crud.getPrintCustomerDeliveryLaw());
        param.setPrintCustomerOrderLaw(crud.getPrintCustomerOrderLaw());
        param.setPrintCustomerQuoteLaw(crud.getPrintCustomerQuoteLaw());
        param.setPrintSupplierAssetLaw(crud.getPrintSupplierAssetLaw());
        param.setPrintSupplierBillLaw(crud.getPrintSupplierBillLaw());
        param.setPrintSupplierOrderLaw(crud.getPrintSupplierOrderLaw());
        JOptionPane.showMessageDialog(null, "Les modifications ont \351t\351 enregistr\351es !");
    }
    private Parameter param;
    private Object list[] = {
        "Agence Raison sociale", "Agence Civilit\351", "Agence Adresse ligne 1", "Agence Adresse ligne 2", "Agence Code postal", "Agence Ville", "Agence Pays", "Agence T\351l", "Agence Fax", "Adresse Raison sociale",
        "Adresse Civilit\351", "Adresse Adresse ligne 1", "Adresse Adresse ligne 2", "Adresse Code postal", "Adresse Ville", "Adresse Pays", "Adresse T\351l", "Adresse Code", "R\351f Numero de la pi\350ce", "R\351f Date",
        "R\351f Commercial", "Colonne R\351f", "Colonne D\351signation", "Colonne Lot", "Colonne Quantit\351", "Colonne Prix vente/achat", "Colonne Remise en %", "Colonne Prix Unitaire HT", "Colonne Montant HT", "Colonne TVA",
        "Totaux Total HT", "Totaux Frais de port", "Totaux Total TVA", "Totaux Total TTC", "Totaux Pay\351", "Totaux Reste du", "Taxes", "Facture client impay\351e", "Ech\351ance de la facture client", "Bas de page Agence Raison social",
        "Bas de page Agence Civilit\351", "Bas de page Agence Notes", "Bas de page Agence Num\351ro siret", "Bas de page Agence Num\351ro naf", "Bas de page Agence Email", "Bas de page Agence Site web"
    };
    private boolean write;
}
